/*
 * @Description: 
 * @Date: 2022-04-03 13:25:48
 * @LastEditTime: 2022-04-06 20:05:15
 * @FilePath: \LisoSever\src\log.c
 */
/*
 * @Description: 
 * @Date: 2022-03-25 16:01:28
 * @LastEditTime: 2022-03-27 22:27:59
 * @FilePath: \LisoSever\src\log.c
 */
#include "log.h"

#define LOG_BUFFER_SIZE 1024 * 1024


// 初始化日志文件
int init_log()
{
    fp = fopen("./run.txt", "w");
    if (fp == NULL)
    {
        printf("[ERROR] log file can not be created");
        return -1;
    }
    return 0;
}

// 写入日志文件
void dump_log(const char *fmt)
{
    time_t tmp = time(NULL);
    struct tm* cur_time = localtime(&tmp);
    if (!cur_time)
    {
        return;
    }
    char arg_buffer[LOG_BUFFER_SIZE];
    strftime(arg_buffer, LOG_BUFFER_SIZE - 1, "[ %x %X ] ", cur_time); 
    fprintf(fp,arg_buffer);
    fprintf(fp,fmt);
    fflush(fp);
}

// 关闭日志文件
void close_log()
{
    fclose(fp);
}
